﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Controler;
using Waiter.Common;

namespace Waiter.CustomControl
{
    public partial class AreaItem : UserControl
    {
        public AreaItem()
        {
            InitializeComponent();
        }
        public AreaItem(int x, int y, int w)
        {
            InitializeComponent();
            this.Location = new System.Drawing.Point(x, y);
            this.Name = "";
            this.Width = w;
        }

        /// <summary>
        /// mã khu vực
        /// </summary>
        private string areaCode = "";

        public string AreaCode
        {
            get { return areaCode; }
            set { areaCode = value; }
        }

        /// <summary>
        /// khởi tạo item khu vực bán và add các table item
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        public void fsLoadAreaItem(MASTER areaItem)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "fsLoadAreaItem", "", "");

            this.areaCode = areaItem.KEY_CODE;
            this.panArea.Text = areaItem.VALUE;

            TableCtrl tableCtrl = new TableCtrl();
            List<TABLE> tableList = tableCtrl.fsGetTableListForDisplay(this.areaCode);
            int pointX = CommonConstants.AREA_PADDING;
            int pointY = CommonConstants.AREA_PADDING;
            for (int i = 0; i < tableList.Count; i++)
            {
                TableItem tableItem = new TableItem(pointX, pointY);
                tableItem.fsLoadTableItem(tableList[i]);
                this.panArea.Controls.Add(tableItem);

                pointX += CommonConstants.TABLE_ITEM_WIDTH + CommonConstants.TABLE_ITEM_MARGIN;
                if (pointX + CommonConstants.TABLE_ITEM_WIDTH > this.Width + CommonConstants.AREA_PADDING)
                {
                    pointX = CommonConstants.AREA_PADDING;
                    pointY += CommonConstants.TABLE_ITEM_HEIGHT + CommonConstants.TABLE_ITEM_MARGIN;
                }
            }
            if (tableList.Count == 0)
            {
                this.Height = CommonConstants.AREA_HEADER_HEIGHT;
            }
            else
            {
                this.Height = pointY + CommonConstants.TABLE_ITEM_HEIGHT + CommonConstants.AREA_HEADER_HEIGHT + CommonConstants.TABLE_ITEM_MARGIN;
            }
        }

        public void fsRefreshAreaItem()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "fsRefreshAreaItem", "", "");

            if (this.panArea.Controls.Count == 0)
            {
                return;
            }
            for (int i = 0; i < this.panArea.Controls.Count; i++)
            {
                TableItem item = (TableItem)this.panArea.Controls[i];
                item.fsRefreshStatus();
            }
        }
    }
}
